@@ -21,6 +21,8 @@ class Event < ActiveRecord::Base |
||
| 21 | 21 |
end |
| 22 | 22 |
|
| 23 | 23 |
def self.cleanup_expired! |
| 24 |
+ affected_agents = Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).group("agent_id").pluck(:agent_id)
|
|
| 24 | 25 |
Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).delete_all
|
| 26 |
+ Agent.where(:id => affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)" |
|
| 25 | 27 |
end |
| 26 | 28 |
end |
@@ -49,7 +49,7 @@ describe EventsController do |
||
| 49 | 49 |
}.should change { Event.count }.by(1)
|
| 50 | 50 |
Event.last.payload.should == events(:bob_website_agent_event).payload |
| 51 | 51 |
Event.last.agent.should == events(:bob_website_agent_event).agent |
| 52 |
- Event.last.created_at.should be_within(1).of(Time.now) |
|
| 52 |
+ Event.last.created_at.to_i.should be_within(2).of(Time.now.to_i) |
|
| 53 | 53 |
end |
| 54 | 54 |
|
| 55 | 55 |
it "can only re-emit Events for the current user" do |
@@ -1,6 +1,7 @@ |
||
| 1 | 1 |
jane_website_agent: |
| 2 | 2 |
type: Agents::WebsiteAgent |
| 3 | 3 |
user: jane |
| 4 |
+ events_count: 1 |
|
| 4 | 5 |
schedule: "5pm" |
| 5 | 6 |
name: "ZKCD" |
| 6 | 7 |
options: <%= {
|
@@ -16,6 +17,7 @@ jane_website_agent: |
||
| 16 | 17 |
bob_website_agent: |
| 17 | 18 |
type: Agents::WebsiteAgent |
| 18 | 19 |
user: bob |
| 20 |
+ events_count: 1 |
|
| 19 | 21 |
schedule: "midnight" |
| 20 | 22 |
name: "ZKCD" |
| 21 | 23 |
options: <%= {
|
@@ -18,20 +18,45 @@ describe Event do |
||
| 18 | 18 |
end |
| 19 | 19 |
|
| 20 | 20 |
describe ".cleanup_expired!" do |
| 21 |
- it "removes any Events whose expired_at date is non-null and in the past" do |
|
| 22 |
- event = agents(:jane_weather_agent).create_event :expires_at => 2.hours.from_now |
|
| 21 |
+ it "removes any Events whose expired_at date is non-null and in the past, updating Agent counter caches" do |
|
| 22 |
+ half_hour_event = agents(:jane_weather_agent).create_event :expires_at => 20.minutes.from_now |
|
| 23 |
+ one_hour_event = agents(:bob_weather_agent).create_event :expires_at => 1.hours.from_now |
|
| 24 |
+ two_hour_event = agents(:jane_weather_agent).create_event :expires_at => 2.hours.from_now |
|
| 25 |
+ three_hour_event = agents(:jane_weather_agent).create_event :expires_at => 3.hours.from_now |
|
| 26 |
+ non_expiring_event = agents(:bob_weather_agent).create_event({})
|
|
| 27 |
+ |
|
| 28 |
+ initial_bob_count = agents(:bob_weather_agent).reload.events_count |
|
| 29 |
+ initial_jane_count = agents(:jane_weather_agent).reload.events_count |
|
| 23 | 30 |
|
| 24 | 31 |
current_time = Time.now |
| 25 | 32 |
stub(Time).now { current_time }
|
| 26 | 33 |
|
| 27 | 34 |
Event.cleanup_expired! |
| 28 |
- Event.find_by_id(event.id).should_not be_nil |
|
| 29 |
- current_time = 119.minutes.from_now |
|
| 35 |
+ Event.find_by_id(half_hour_event.id).should_not be_nil |
|
| 36 |
+ Event.find_by_id(one_hour_event.id).should_not be_nil |
|
| 37 |
+ Event.find_by_id(two_hour_event.id).should_not be_nil |
|
| 38 |
+ Event.find_by_id(three_hour_event.id).should_not be_nil |
|
| 39 |
+ Event.find_by_id(non_expiring_event.id).should_not be_nil |
|
| 40 |
+ agents(:bob_weather_agent).reload.events_count.should == initial_bob_count |
|
| 41 |
+ agents(:jane_weather_agent).reload.events_count.should == initial_jane_count |
|
| 42 |
+ |
|
| 43 |
+ current_time = 119.minutes.from_now # move almost 2 hours into the future |
|
| 30 | 44 |
Event.cleanup_expired! |
| 31 |
- Event.find_by_id(event.id).should_not be_nil |
|
| 32 |
- current_time = 2.minutes.from_now |
|
| 45 |
+ Event.find_by_id(half_hour_event.id).should be_nil |
|
| 46 |
+ Event.find_by_id(one_hour_event.id).should be_nil |
|
| 47 |
+ Event.find_by_id(two_hour_event.id).should_not be_nil |
|
| 48 |
+ Event.find_by_id(three_hour_event.id).should_not be_nil |
|
| 49 |
+ Event.find_by_id(non_expiring_event.id).should_not be_nil |
|
| 50 |
+ agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1 |
|
| 51 |
+ agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 1 |
|
| 52 |
+ |
|
| 53 |
+ current_time = 2.minutes.from_now # move 2 minutes further into the future |
|
| 33 | 54 |
Event.cleanup_expired! |
| 34 |
- Event.find_by_id(event.id).should be_nil |
|
| 55 |
+ Event.find_by_id(two_hour_event.id).should be_nil |
|
| 56 |
+ Event.find_by_id(three_hour_event.id).should_not be_nil |
|
| 57 |
+ Event.find_by_id(non_expiring_event.id).should_not be_nil |
|
| 58 |
+ agents(:bob_weather_agent).reload.events_count.should == initial_bob_count - 1 |
|
| 59 |
+ agents(:jane_weather_agent).reload.events_count.should == initial_jane_count - 2 |
|
| 35 | 60 |
end |
| 36 | 61 |
|
| 37 | 62 |
it "doesn't touch Events with no expired_at" do |